//
//  NewDetailViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewDetailViewController.h"
#import "MusicPlyController.h"

#import "NewDetailTableViewCell.h"

#import "SongListModel.h"
#import "UrlListModel.h"
//#import "NewDetailModel.h"

@interface NewDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *songsArray;
@property (nonatomic,strong) UIImageView *image;

@end

@implementation NewDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
   
    
    
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9C9C9C);
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    
    
    self.dataArr = [NSMutableArray array];
    self.songsArray = [NSMutableArray array];
    // 设置导航栏透明
    [self.navigationController.navigationBar setTranslucent:YES];
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)]) {
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        
    }
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
    // 适应导航栏 重设tableview的坐标
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[NewDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
//    [self createheaderImage];
    [self getData];
    
    
    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
       self.image.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.tableView.tableHeaderView = self.image;
}
- (void)getData
{

    NSString *urlStr = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100",self.albumId];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        NSMutableArray *songArray = [dic objectForKey:@"songList"];
        for (NSDictionary *smallDic in songArray) {
            SongListModel *model = [SongListModel new];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataArr addObject:model];
            
            [self.image sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"hold.png"]];

        }
        
        
        
        
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}



//- (void)createheaderImage
//{
//    NSMutableArray *picArray = [NSMutableArray array];
//    for (SongListModel *model in self.dataArr) {
//        [picArray addObject:model.picUrl];
//    }
//    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
//
//    self.image.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
////    self.tableView.tableHeaderView = self.image;
//    
//
//    
//}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    SongListModel *model = self.dataArr[indexPath.row];

    cell.selectionStyle = UITableViewCellAccessoryNone;

    cell.number.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    cell.name.text = model.singerName;
    cell.singerName.text = model.name;
    
 
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicPlyController *musicPlay = [MusicPlyController musicplyVC];
    NSMutableArray *Array = [NSMutableArray array];
    for (SongListModel *listMOdel in self.dataArr) {
        UrlListModel *urlModel = [listMOdel.urlList lastObject];
        [Array addObject:urlModel.url];
    }
    musicPlay.MusicArray = [NSMutableArray arrayWithArray:Array];
    musicPlay.index = indexPath.row;
    
    [self presentViewController:musicPlay animated:YES completion:^{
        
        
    }];
}

@end
